package com.youdoneed.community.shortVideo.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.github.pagehelper.PageHelper;
import com.youdoneed.base.application.AppException;
import com.youdoneed.community.common.utils.StringUUIdUtil;
import com.youdoneed.community.shortVideo.dao.MusiclibraryMapper;
import com.youdoneed.community.shortVideo.dao.MusicrecommendMapper;
import com.youdoneed.community.shortVideo.dao.MusictypeMapper;
import com.youdoneed.community.shortVideo.dao.MusictyperelMapper;
import com.youdoneed.community.shortVideo.entity.Musiclibrary;
import com.youdoneed.community.shortVideo.entity.Musicrecommend;
import com.youdoneed.community.shortVideo.entity.Musictype;
import com.youdoneed.community.shortVideo.entity.Musictyperel;

@Service
public class MusictypeService {
	
	private final Logger logger = LoggerFactory.getLogger(MusictypeService.class);
	
	private final String musicLibraryType = "2aeded1129784decbab8b8bf18329c7c";
	
	@Autowired
	MusictypeMapper musictypeMapper;
	
	@Autowired
	MusictyperelMapper musictyperelMapper;
	
	@Autowired
	MusiclibraryMapper musiclibraryMapper;
	
	@Autowired
	MusicrecommendMapper musicrecommendMapper;
	
	
	/**
	 * 添加音乐分类
	 * @param musictype
	 * @author lixs
	 * @Date 2017.8.31
	 */
	@Transactional
	public int addMusicType(Musictype musictype){
		if(null==musictype){
			throw new AppException("音乐分类不能为空");
		}
		//找出音乐分类的数量
		//int count = musictypeMapper.countMusicType();
		//保存音乐分类
		Musictype musictypeNew = new Musictype();
		musictypeNew.setTypeId(StringUUIdUtil.getUUID());
		musictypeNew.setCreateDate(new Date());
		musictypeNew.setIsHide(musictype.getIsHide());
		musictypeNew.setTypeIcon(musictype.getTypeIcon());
		musictypeNew.setTypeName(musictype.getTypeName());
		//用户排序
		musictypeNew.setSort(musictype.getSort());
		return musictypeMapper.insertSelective(musictypeNew);
	}
	
	/**
	 * 删除音乐分类
	 * @param typeId
	 * @author lixs
	 * @Date 2017.8.31
	 */
	@Transactional
	public int deleteMusicType(String typeId){
		if(!StringUtils.hasText(typeId)){
			throw new AppException("音乐分类id不能为空");
		}
		Musictype musictype = musictypeMapper.selectByPrimaryKey(typeId);
		if(null==musictype){
			throw new AppException("未找到该音乐分类");
		}
		return musictypeMapper.deleteByPrimaryKey(typeId);
	}
	
	/**
	 * 更新音乐分类
	 * @param musictype
	 * @author lixs
	 * @Date 2017.8.31
	 */
	@Transactional
	public int updateMusicType(Musictype musictype){
		if(null==musictype){
			throw new AppException("音乐分类不能为空");
		}
		Musictype musictypeNew = musictypeMapper.selectByPrimaryKey(musictype.getTypeId());
		if(null==musictypeNew){
			throw new AppException("未找到该音乐分类");
		}
		musictypeNew.setIsHide(musictype.getIsHide());
		musictypeNew.setSort(musictype.getSort());
		musictypeNew.setTypeIcon(musictype.getTypeIcon());
		musictypeNew.setTypeName(musictype.getTypeName());
		return musictypeMapper.updateByPrimaryKeySelective(musictypeNew);
	}
	
	/**
	 * 查询所有音乐分类(后台专用)
	 * @param currentPage
	 * @param rows
	 * @author lixs
	 * @Date 2017.10.12
	 */
	public List<Musictype> findMusictypeList(Boolean mark,Integer currentPage,Integer rows){
		
		PageHelper.startPage(currentPage, rows);
		//返回true,则查询所有分类
		if(true==mark){
			return musictypeMapper.findMusictypeList();
		}
		//返回false则查询未隐藏的分类,并且去除首页热点音乐
		List<Musictype> musictype = musictypeMapper.findPublicMusictypeList();
		List<Musictype> musictypeList = new ArrayList<>();
		for(Musictype musictypeNew:musictype){
			if(musictypeNew.getTypeId().equals(musicLibraryType)){
				continue;
			}
			musictypeList.add(musictypeNew);
		}
		return musictypeList;
		
	}
	
	
	/**
	 * 查询音乐分类下的所有音乐
	 * @param typeId
	 * @param currentPage
	 * @param rows
	 * @author lixs
	 * @Date 2017.9.1
	 */
	public List<Musiclibrary> findTypeMusic(String typeId,Integer currentPage,Integer rows){
		
		if(!StringUtils.hasText(typeId)){
			throw new AppException("音乐类型id为空");
		}
		PageHelper.startPage(currentPage, rows);
		
		List<Musiclibrary> musiclibrary = musictyperelMapper.findTypeMusic(typeId);
		if(null==musiclibrary){
			throw new AppException("该分类下没有音乐");
		}
		return musiclibrary;
	}
	
	
	public int findTypeMusicForTotal(String typeId){
		if(!StringUtils.hasText(typeId)){
			throw new AppException("音乐类型id为空");
		}
		return musictyperelMapper.findTypeMusicForTotal(typeId);
	}
	 
	/**
	 * 批量向音乐分类下添加音乐
	 * @param musicId
	 * @author lixs
	 * @Date 2017.9.1
	 */
	@Transactional
	public Boolean addMusicToType(List<Musiclibrary> musiclibrary,String typeId){
		if(null==musiclibrary){
			throw new AppException("音乐列表不能为空");
		}
		if(!StringUtils.hasText(typeId)){
			throw new AppException("音乐分类id不能为空");	
		}
		List<Musictyperel> musicTyperel = new ArrayList<>();
//		IntStream.range(0,musiclibrary.size()).forEach(value ->{
//				musicTyperel.get(value).setMusicId(musiclibrary.get(value).getMusicId());
//				musicTyperel.get(value).setMusicTypeRelId(StringUUIdUtil.getUUID());
//				musicTyperel.get(value).setPlayCount(0);
//				musicTyperel.get(value).setTypeId(typeId);
//				musicTyperel.get(value).setCreateDate(new Date());
//				musictyperelMapper.insert(musicTyperel.get(value));
//				});
		for(int index=0;index<musiclibrary.size();index++){
			musicTyperel.get(index).setMusicId(musiclibrary.get(index).getMusicId());
			musicTyperel.get(index).setMusicTypeRelId(StringUUIdUtil.getUUID());
			musicTyperel.get(index).setPlayCount(0);
			musicTyperel.get(index).setTypeId(typeId);
			musicTyperel.get(index).setCreateDate(new Date());
			musictyperelMapper.insert(musicTyperel.get(index));
		}
		return true;
	}
	
	/**
	 * 通过歌曲名或者歌手名模糊查询音乐分类下的音乐
	 * @param typeId
	 * @param name
	 * @param currentPage
	 * @param rows
	 * @author lixs
	 * @Date 2017.9.18
	 */
	public List<Musiclibrary> findTypeMusicByName(String typeId,String name,Integer currentPage,Integer rows){
		if(!StringUtils.hasText(typeId)){
			throw new AppException("请先选择一个分类");
		}
		PageHelper.startPage(currentPage,rows);
		return musictyperelMapper.findTypeMusicByName(typeId, name);
	}
	
	public int findTypeMusicByNameForTotal(String typeId,String name){
		if(!StringUtils.hasText(typeId)){
			throw new AppException("请先选择一个分类");
		}
		return musictyperelMapper.findTypeMusicByNameForTotal(typeId, name);
	}
	
	/**
	 * 向音乐分类下批量添加音乐
	 * @param vo
	 * @author lixs
	 * @Date 2017.9.18
	 */
	@Transactional
	public Boolean addMusicToTypeBatch(List<String> musicId,String typeId){
		
		if(!StringUtils.hasText(typeId)){
			throw new AppException("您未选择任何音乐分类");
		}
		
		if(null==musicId){
			throw new AppException("您未选择任何音乐");
		}
		
		Musictype musicType = musictypeMapper.selectByPrimaryKey(typeId);
		if(null==musicType){
			throw new AppException("未找到该音乐分类");
		}
		
		for(String musicid:musicId){
			
			Musictyperel musictyperelNew = musictyperelMapper.findUniqueTypeMusic(typeId, musicid);
			if(null!=musictyperelNew){
				logger.info("该音乐已在此分类下");
				continue;
			}
			
			Musictyperel musictyperel = new Musictyperel();
			musictyperel.setMusicId(musicid);
			musictyperel.setMusicTypeRelId(StringUUIdUtil.getUUID());
			musictyperel.setPlayCount(0);
			musictyperel.setTypeId(typeId);
			musictyperel.setCreateDate(new Date());
			musictyperelMapper.insertSelective(musictyperel);
		}
		return true;
	}
	
	/**
	 * 从音乐分类中批量删除音乐
	 * @param musiclibrary
	 * @param typeId
	 * @author lixs
	 * @Date 2017.9.18
	 */
	@Transactional
	public Boolean deleteMusicToTypeBatch(List<Musictyperel> musictyperel){
		if(null==musictyperel){
			throw new AppException("您未选择任何音乐");
		}
		for(Musictyperel rel : musictyperel){
			logger.info("删除分类下的音乐");
			musictyperelMapper.deleteByPrimaryKey(rel.getMusicTypeRelId());

			
			logger.info("如果分类下的歌在推荐列表中，也将推荐音乐删除");
			Musicrecommend musicRecommend = musicrecommendMapper.findTheUnique(rel.getMusicId(), rel.getTypeId());
			if(null!=musicRecommend){
				logger.info(musicRecommend.getMusicId()+"音乐id");
				musicrecommendMapper.deleteByPrimaryKey(musicRecommend.getRecommendMusicTypeId());
			}
			continue;
			
		}
		return true;
	}
	
	/**
	 * 将音乐复制到另一音乐分类中
	 * @param musicId
	 * @param typeId
	 * @author lixs
	 * @Date 2017.9.21
	 */
	@Transactional
	public Boolean copyMusicToAnotherType(List<String> musicId,String typeId){
		if(null==musicId){
			throw new AppException("您未选择任何音乐");
		}
		if(!StringUtils.hasText(typeId)){
			throw new AppException("您未选择音乐分类");
		}
		Musictype musictype = musictypeMapper.selectByPrimaryKey(typeId);
		if(null==musictype){
			throw new AppException("未找到音乐分类");
		}
		for(int index=0;index<musicId.size();index++){
			
			Musictyperel musictyperelNew = musictyperelMapper.findUniqueTypeMusic(typeId, musicId.get(index));
			if(null!=musictyperelNew){
				logger.info("该分类下中已有这首歌");
				continue;
			}
			Musictyperel musictyperel = new Musictyperel();
			musictyperel.setMusicId(musicId.get(index));
			musictyperel.setMusicTypeRelId(StringUUIdUtil.getUUID());
			musictyperel.setPlayCount(0);
			musictyperel.setTypeId(typeId);
			musictyperel.setCreateDate(new Date());
			musictyperelMapper.insertSelective(musictyperel);
		}
		return true;
	}
	
	/**
	 * 将音乐剪切到另一音乐分类中
	 * @param musicId
	 * @param typeId
	 * @author lixs
	 * @Date 2017.9.21
	 */
	@Transactional
	public Boolean cutMusicToAnotherType(List<Musictyperel> musictyperels,String typeId){
		if(null==musictyperels){
			throw new AppException("您未选择任何音乐");
		}
		if(!StringUtils.hasText(typeId)){
			throw new AppException("您未选择音乐分类");
		}
		Musictype musictype = musictypeMapper.selectByPrimaryKey(typeId);
		if(null==musictype){
			throw new AppException("未找到音乐分类");
		}
		for(int index=0;index<musictyperels.size();index++){
			logger.info("循环次数"+index);
			Musictyperel musictyperelNew = musictyperelMapper.findUniqueTypeMusic(typeId, musictyperels.get(index).getMusicId());
			if(null!=musictyperelNew){
				logger.info("该分类下中已有这首歌");
				continue;
			}
			Musictyperel musictyperel = new Musictyperel();
			musictyperel.setMusicId(musictyperels.get(index).getMusicId());
			musictyperel.setMusicTypeRelId(StringUUIdUtil.getUUID());
			musictyperel.setPlayCount(0);
			musictyperel.setTypeId(typeId);
			musictyperel.setCreateDate(new Date());
			musictyperelMapper.insertSelective(musictyperel);
			
			//同时删除本分类下的音乐
			musictyperelMapper.deleteByPrimaryKey(musictyperels.get(index).getMusicTypeRelId());
			//如果该分类的音乐作为推荐音乐，同时也将删除该分类下的推荐音乐
			Musicrecommend musicrecommend = musicrecommendMapper.findTheUnique(musictyperels.get(index).getMusicId(), musictyperels.get(index).getTypeId());
			if(null!=musicrecommend){
				logger.info("MusicTypeId:"+musicrecommend.getRecommendMusicTypeId());
				musicrecommendMapper.deleteByPrimaryKey(musicrecommend.getRecommendMusicTypeId());
			}
			continue;
		}
		return true;
	}
	
	public int countMusicType(){
		return musictypeMapper.countMusicType();
	}
	
	public int countMusicTypeForPublic(){
		return musictypeMapper.countMusicTypeForPublic();
	}

}
